package com.wymobilearchitecture.study.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.wymobilearchitecture.R
import com.wy.common.base.BaseActivity

/**
 * Fragment 源码分析使用
 */
class TestFragmentActivity : BaseActivity(){

    private val TAG_FIRST_FRAGMENT = "first_fragment"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.test_fragment_actisvity)

        val firstFragment = TestFragmentFirst()
        val secondFragment = TestFragmentSecond()
        val thirdFragment = TestFragmentThird()

        val fragmentList : ArrayList<Fragment> = ArrayList()
        fragmentList.add(firstFragment)
        fragmentList.add(secondFragment)
        fragmentList.add(thirdFragment)

        supportFragmentManager
            .beginTransaction()
            .add(R.id.container,secondFragment)
            .replace(R.id.container,thirdFragment)
            .remove(thirdFragment)
            .remove(secondFragment)
            .add(R.id.container,firstFragment)
            .commit()


//        val viewPager : CViewPager = findViewById(R.id.view_pager)
//        viewPager.setAdapter(MAdapter(supportFragmentManager,fragmentList))

        //fragment 页面重叠问题
//        val fragment : Fragment? = supportFragmentManager.findFragmentByTag(TAG_FIRST_FRAGMENT)
//        if (fragment == null){
//            supportFragmentManager
//                .beginTransaction()
//                .add(R.id.container,secondFragment,TAG_FIRST_FRAGMENT)
//                .commit()
//        }


    }

    override fun onResume() {
        super.onResume()
    }


    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)

    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
    }

    class MAdapter(fm: FragmentManager,list: ArrayList<Fragment>) : FragmentPagerAdapter(fm) {


        private val mList = list

        override fun getItem(position: Int): Fragment {
           return mList[position]
        }

        override fun getCount(): Int {
            return mList.size
        }


    }

}